import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_yyt/models/floor_list_entity.dart';
import 'package:ls_work/model/building_model.dart';
import 'package:ls_work/net/response/warning_type_list_data.dart';

import '../../config/global_configs.dart';
import '../../model/warning/warning_model.dart';

class SetWarningInfoViewModel extends BaseViewModel {
  SetWarningInfoViewModel(BuildContext context) : super(context);

  var buildingModel = BuildingModel();
  var warningModel = WarningModel();

  var warningTypeList = <WarningTypeListData>[];
  var buildingList = <FloorEntity>[];

  var selectTypes = <num>[];
  var selectBuilding = <SosBuildingData>[];

  Future<void> getWarningList() async {
    showLoadingDialog();
    try {
      buildingList = await buildingModel.getBuildingList(page: 1, rows: 100,isFilter: 0);
      warningTypeList = await warningModel.getWarningTypeList();
      hideDialog();
      notifyListeners();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    _initSavedData();
    getWarningList();
  }

  void _initSavedData(){
    selectTypes.addAll(GlobalConfigs.selectWarningType);
    print(selectTypes);
    selectBuilding.addAll(GlobalConfigs.selectBuildingAndFloor);
  }

  @override
  void dispose() {
    super.dispose();
    selectTypes.clear();
    selectBuilding.clear();
  }

  bool isInSelectBuildingFloor(dynamic buildingId,dynamic floorId){
    var getBuilding =  selectBuilding.where((element) => element.buildingId==buildingId).toList();
    if(getBuilding.isEmpty){
      return false;
    }
    return (getBuilding.first.floorIds??[]).contains(floorId);
  }

  SosBuildingData?  findedBuilding(dynamic buildingId) {
    var getBuilding =  selectBuilding.where((element) => element.buildingId==buildingId).toList();
    if(getBuilding.isEmpty){
      return null;
    }else{
      return getBuilding.first;
    }
  }

  IconData buildingCheckIcon(FloorEntity building){
    if((building.floorList??[]).isEmpty){
      return findedBuilding(building.id)!=null?Icons.check_box:Icons.check_box_outline_blank;
    }else{
      return (findedBuilding(building.id)?.floorIds??[]).length==(building.floorList??[]).length?
      Icons.check_box:Icons.check_box_outline_blank;
    }
  }

  bool buildingChecked(FloorEntity building){
    if((building.floorList??[]).isEmpty){
      return findedBuilding(building.id)!=null;
    }else{
      return (findedBuilding(building.id)?.floorIds??[]).length==(building.floorList??[]).length;
    }
  }

  void chooseAll(){
    selectBuilding =  buildingList.map((e) => SosBuildingData(buildingId: e.id,floorIds: (e.floorList??[]).map((e) => e.id).toList())).toList();
    notifyListeners();
  }

  void clearAll(){
    selectBuilding = [];
    notifyListeners();
  }

  bool isAllBuildingChecked(){
    var allCheck = true;
    for(var building in buildingList){
      var bl = buildingChecked(building);
      if(!bl){
        allCheck = false;
        break;
      }
    }
    return allCheck;
  }

  Future<void> saveSetting() async {
    await GlobalConfigs.saveWarningInfo(selectTypes, selectBuilding);
    finish(data: true);
  }
}
